package br.edu.ufc.deti.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.regex.Pattern;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class SshClient {
	BufferedReader fromServer;
	Pattern alphaNumeric = Pattern.compile("([^a-zA-z0-9])");
	OutputStream toServer;
	String lastCommand = "";
	Channel channel;

	private static final String TERMINATOR = "zDonez";

	public void connect(String username, String password, String host, int port) throws JSchException, IOException {
		JSch shell = new JSch();
		Session session = shell.getSession(username, host, port);
		MyUserInfo ui = new MyUserInfo();
		ui.setPassword(password);
		session.setUserInfo(ui);
		session.connect();

		channel = session.openChannel("exec");
		fromServer = new BufferedReader(new InputStreamReader(channel.getInputStream()));
		toServer = channel.getOutputStream();
		channel.connect();
		//if(isConnected()){
		//	send("echo \"\"");
		//}
	}

	public boolean isConnected() {
		return (channel != null && channel.isConnected());
	}

	public void disconnect() {
		if (isConnected()) {
			channel.disconnect();
		}
	}

	public void send(String command) throws IOException {
		//command += "; echo \"" + TERMINATOR + "\"\n";
		toServer.write(command.getBytes());
		lastCommand = new String(command);
	}

	public String getServerResponse() throws IOException, InterruptedException {
		String line = "";
        while((line = fromServer.readLine()) != null){
            System.out.println(line);
         }

/*		StringBuilder builder = new StringBuilder();
		int count = 0;
		for (int i = 0; true; i++) {
			line = fromServer.readLine();
			builder.append(line).append("\n");
			if (line.contains(TERMINATOR) && (++count > 1)) {
				break;
			}
		}
		String result = builder.toString();
		int beginIndex = result.indexOf(TERMINATOR+"\"") + ((TERMINATOR+"\"").length());
		result = result.substring(beginIndex);
		return result.replaceAll(escape(TERMINATOR), "").trim();
*/	
        return line;
	}
	private String escape(String subjectString){
		return alphaNumeric.matcher(subjectString).replaceAll("\\\\$1");
	}
	private static class MyUserInfo implements UserInfo {
		private String password;

		public void setPassword(String password) {
			this.password = password;
		}

		public String getPassphrase() {
			return null;
		}

		public String getPassword() {
			return password;
		}

		public boolean promptPassword(String arg0) {
			return true;
		}

		public boolean promptPassphrase(String arg0) {
			return true;
		}

		public boolean promptYesNo(String arg0) {
			return true;
		}

		public void showMessage(String arg0) {
			System.out.println(arg0);
		}
	}
	public static void main(String[] args) throws JSchException, IOException, InterruptedException {
		SshClient client = new SshClient();
		client.connect("antonio", "rafael", "192.168.1.77", 22);
		client.send("ls -l");
		System.out.println(client.getServerResponse());
	}
}
